<template>
  <div class="app-container">
    <el-form class="formWidth" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="现就业状况" prop="employmentSituation">
        <el-select v-model="queryParams.employmentSituation" placeholder="请选择现就业状况" clearable size="small">
          <el-option
            v-for="dict in employmentSituationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="个人年收入" prop="annualRevenue">
        <el-select v-model="queryParams.annualRevenue" placeholder="请选择个人年收入" clearable size="small">
          <el-option
            v-for="dict in annualRevenueOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="住房公积金" prop="houseFunds">
        <el-select v-model="queryParams.houseFunds" placeholder="请选择住房公积金" clearable size="small">
          <el-option
            v-for="dict in houseFundsOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="社会救助" prop="socialAssistance">
        <el-select v-model="queryParams.socialAssistance" placeholder="请选择社会救助" clearable size="small">
          <el-option
            v-for="dict in socialAssistanceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="养老保险" prop="endowmentInsurance">
        <el-select v-model="queryParams.endowmentInsurance" placeholder="请选择养老保险" clearable size="small">
          <el-option
            v-for="dict in endowmentInsuranceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="医疗保险" prop="medicalInsurance">
        <el-select v-model="queryParams.medicalInsurance" placeholder="请选择医疗保险" clearable size="small">
          <el-option
            v-for="dict in medicalInsuranceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="住房性质" prop="houseProperty">
        <el-select v-model="queryParams.houseProperty" placeholder="请选择住房性质" clearable size="small">
          <el-option
            v-for="dict in housePropertyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:retirefm_3:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manager:retirefm_3:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manager:retirefm_3:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['manager:retirefm_3:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="retirefm_3List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" :show-overflow-tooltip="true" align="center" prop="soldierName" />
      <el-table-column label="公司名称" :show-overflow-tooltip="true" align="center" prop="companyName" />
      <el-table-column label="现就业状况" :show-overflow-tooltip="true" align="center" prop="employmentSituation" :formatter="employmentSituationFormat" />
      <el-table-column label="个人年收入" :show-overflow-tooltip="true" align="center" prop="annualRevenue" :formatter="annualRevenueFormat" />
      <el-table-column label="住房公积金" :show-overflow-tooltip="true" align="center" prop="houseFunds" :formatter="houseFundsFormat" />
      <el-table-column label="社会救助" :show-overflow-tooltip="true" align="center" prop="socialAssistance" :formatter="socialAssistanceFormat" />
      <el-table-column label="养老保险" :show-overflow-tooltip="true" align="center" prop="endowmentInsurance" :formatter="endowmentInsuranceFormat" />
      <el-table-column label="医疗保险" :show-overflow-tooltip="true" align="center" prop="medicalInsurance" :formatter="medicalInsuranceFormat" />
      <el-table-column label="住房性质" :show-overflow-tooltip="true" align="center" prop="houseProperty" :formatter="housePropertyFormat" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:retirefm_3:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:retirefm_3:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改就业创业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form class="formWidth" ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20" >
          <el-col :span="12" >
            <el-form-item label="身份证号" prop="soldierId" v-if="!isUpdate">
              <el-select
                v-model="form.soldierId"
                filterable
                remote
                reserve-keyword
                placeholder="输入身份证号系统自动匹配"
                :remote-method="remoteMethod"
                :loading="idLoading">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="现就业状况" prop="employmentSituation">
              <el-select v-model="form.employmentSituation" placeholder="请选择现就业状况">
                <el-option
                  v-for="dict in employmentSituationOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->
<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="个人年收入" prop="annualRevenue">
              <el-select v-model="form.annualRevenue" placeholder="请选择个人年收入">
                <el-option
                  v-for="dict in annualRevenueOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="住房公积金" prop="houseFunds">
              <el-select v-model="form.houseFunds" placeholder="请选择住房公积金">
                <el-option
                  v-for="dict in houseFundsOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->
<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="社会救助" prop="socialAssistance">
              <el-select v-model="form.socialAssistance" placeholder="请选择社会救助">
                <el-option
                  v-for="dict in socialAssistanceOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="养老保险" prop="endowmentInsurance">
              <el-select v-model="form.endowmentInsurance" placeholder="请选择养老保险">
                <el-option
                  v-for="dict in endowmentInsuranceOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->
<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="医疗保险" prop="medicalInsurance">
              <el-select v-model="form.medicalInsurance" placeholder="请选择医疗保险">
                <el-option
                  v-for="dict in medicalInsuranceOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="住房性质" prop="houseProperty">
              <el-select v-model="form.houseProperty" placeholder="请选择住房性质">
                <el-option
                  v-for="dict in housePropertyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--        </el-row>-->
<!--        <el-row :gutter="20" >-->
          <el-col :span="12" >
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12" ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRetirefm_3, getRetirefm_3, delRetirefm_3, addRetirefm_3, updateRetirefm_3, exportRetirefm_3 } from "@/api/manager/retirefm_3";
import { getBaseInfoByID } from "@/api/manager/retirefm_1";
export default {
  name: "Retirefm_3",
  data() {
    return {
      idLoading: false,
      // 身份证列表
      options: [],
      isUpdate:false,

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 就业创业表格数据
      retirefm_3List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 现就业状况字典
      employmentSituationOptions: [],
      // 个人年收入字典
      annualRevenueOptions: [],
      // 住房公积金字典
      houseFundsOptions: [],
      // 社会救助状况字典
      socialAssistanceOptions: [],
      // 养老保险字典
      endowmentInsuranceOptions: [],
      // 医疗保险字典
      medicalInsuranceOptions: [],
      // 住房性质字典
      housePropertyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employmentSituation: null,
        annualRevenue: null,
        houseFunds: null,
        socialAssistance: null,
        endowmentInsurance: null,
        medicalInsurance: null,
        houseProperty: null,
        companyName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        soldierId: [
          {
            required: true,
            message: "军人身份证号不能为空",
            trigger: "blur",
          },
        ],
        employmentSituation: [
          { required: true, message: "现就业状况不能为空", trigger: "change" }
        ],
        annualRevenue: [
          { required: true, message: "个人年收入不能为空", trigger: "change" }
        ],
        houseFunds: [
          { required: true, message: "住房公积金不能为空", trigger: "change" }
        ],
        socialAssistance: [
          { required: true, message: "社会救助不能为空", trigger: "change" }
        ],
        endowmentInsurance: [
          { required: true, message: "养老保险不能为空", trigger: "change" }
        ],
        medicalInsurance: [
          { required: true, message: "医疗保险不能为空", trigger: "change" }
        ],
        houseProperty: [
          { required: true, message: "住房性质不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("employment_situation").then(response => {
      this.employmentSituationOptions = response.data;
    });
    this.getDicts("annual_revenue").then(response => {
      this.annualRevenueOptions = response.data;
    });
    this.getDicts("have_or_no").then(response => {
      this.houseFundsOptions = response.data;
    });
    this.getDicts("social_assistance").then(response => {
      this.socialAssistanceOptions = response.data;
    });
    this.getDicts("endowment_insurance").then(response => {
      this.endowmentInsuranceOptions = response.data;
    });
    this.getDicts("medical_insurance").then(response => {
      this.medicalInsuranceOptions = response.data;
    });
    this.getDicts("house_property").then(response => {
      this.housePropertyOptions = response.data;
    });
  },
  methods: {
    /**通过身份证查询基本信息*/
    remoteMethod(id) {
      if (id != "") {
        let params = {
          idNumber: id,
        };
        this.options = [];
        getBaseInfoByID(params)
          .then((res) => {
            if (res.rows == null || res.rows == undefined) {
              this.options = [];
              return;
            }
            this.options = res.rows.map((item) => {
              return {
                value: item.id,
                label: item.idNumber,
              };
            });
          })
          .catch((err) => console.error(err));
      } else {
        this.options = [];
      }
    },

    /** 查询就业创业列表 */
    getList() {
      this.loading = true;
      listRetirefm_3(this.queryParams).then(response => {
        this.retirefm_3List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 现就业状况字典翻译
    employmentSituationFormat(row, column) {
      return this.selectDictLabel(this.employmentSituationOptions, row.employmentSituation);
    },
    // 个人年收入字典翻译
    annualRevenueFormat(row, column) {
      return this.selectDictLabel(this.annualRevenueOptions, row.annualRevenue);
    },
    // 住房公积金字典翻译
    houseFundsFormat(row, column) {
      return this.selectDictLabel(this.houseFundsOptions, row.houseFunds);
    },
    // 社会救助状况字典翻译
    socialAssistanceFormat(row, column) {
      return this.selectDictLabel(this.socialAssistanceOptions, row.socialAssistance);
    },
    // 养老保险字典翻译
    endowmentInsuranceFormat(row, column) {
      return this.selectDictLabel(this.endowmentInsuranceOptions, row.endowmentInsurance);
    },
    // 医疗保险字典翻译
    medicalInsuranceFormat(row, column) {
      return this.selectDictLabel(this.medicalInsuranceOptions, row.medicalInsurance);
    },
    // 住房性质字典翻译
    housePropertyFormat(row, column) {
      return this.selectDictLabel(this.housePropertyOptions, row.houseProperty);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        soldierId: null,
        employmentSituation: null,
        annualRevenue: null,
        houseFunds: null,
        socialAssistance: null,
        endowmentInsurance: null,
        medicalInsurance: null,
        houseProperty: null,
        companyName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isUpdate = false;
      this.reset();
      this.open = true;
      this.title = "添加就业创业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isUpdate = true;
      this.reset();
      const id = row.id || this.ids
      getRetirefm_3(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改就业创业";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRetirefm_3(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRetirefm_3(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该项数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRetirefm_3(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有就业创业数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRetirefm_3(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
